package com.own.testapp.callback;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ProgressBar;

import com.okgo.callback.StringCallback;
import com.okgo.request.base.Request;
import com.own.testapp.R;

public abstract class StringDialogCallback extends StringCallback
{

    private Dialog dialog;

    public StringDialogCallback(Activity activity) {
        dialog = new Dialog(activity, R.style.progress_dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.ios_dialog);
        dialog.setCancelable(true);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }

    public StringDialogCallback(Activity activity, int progressBarColor) {
        dialog = new Dialog(activity, R.style.progress_dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(activity).inflate(R.layout.ios_dialog, null);
        ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progress_bar_ios);
        progressBar.getIndeterminateDrawable().setColorFilter(progressBarColor, android.graphics.PorterDuff.Mode.SRC_IN);
        dialog.setContentView(view);
        dialog.setCancelable(true);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }

    public StringDialogCallback(Activity activity, int progressBarColor, boolean cancelble) {
        dialog = new Dialog(activity, R.style.progress_dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(activity).inflate(R.layout.ios_dialog, null);
        ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progress_bar_ios);
        progressBar.getIndeterminateDrawable().setColorFilter(progressBarColor, android.graphics.PorterDuff.Mode.SRC_IN);
        dialog.setContentView(view);
        dialog.setCancelable(cancelble);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }


    @Override
    public void onStart(Request<String, ? extends Request> request) {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    @Override
    public void onFinish() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void onError(com.okgo.model.Response<String> response)
    {
        super.onError(response);
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}
